package com.dmooo.yxmmerchant.view.presenter;

import android.content.Context;
import android.util.Log;

import com.common.mvpbase.BasePresenter;
import com.common.net.listener.HttpOnNextListener;
import com.common.util.SPUtils;
import com.common.wx.HttpUtils;
import com.dmooo.yxmmerchant.common.Config;
import com.dmooo.yxmmerchant.view.contract.LoginContract;
import com.dmooo.yxmmerchant.view.model.LoginModel;
import com.google.gson.GsonBuilder;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

public class LoginPrestener extends BasePresenter<LoginContract.LoginView> implements LoginContract.LoginPtr {
    private LoginModel model;
    private Context context;
    GsonBuilder builder=new GsonBuilder();
    public LoginPrestener(LoginContract.LoginView view, Context context) {
        super(view);
        model = new LoginModel(context);
        this.context=context;
        builder.serializeNulls();
    }
    @Override
    public void doLogin(final String account,final String pwd) {
        model.doLogin(account, pwd, new HttpOnNextListener() {
            @Override
            public void onNext(Object o) {
                try {
                    JSONObject object=new JSONObject(builder.create().toJson(o));
                    SPUtils.put(context,Config.PHONE,account);
                    SPUtils.put(context,Config.PWD,pwd);
                    Config.AUTH="";
                    SPUtils.put(context,Config.TOKEN,object.getString("token"));
                    SPUtils.put(context,Config.UID,object.getString("merchant_id"));
                    SPUtils.put(context,Config.STATUS,object.getString("status"));
                    SPUtils.put(context,Config.GROUP_ID,object.getString("merchant_group_id"));
                    SPUtils.put(context,Config.CHECK_REASON,object.getString("check_reason"));
                    mView.loginSuccess();
                } catch (JSONException e) {
                    e.printStackTrace();
                    mView.errorMsg("数据解析错误");
                }
            }

            @Override
            public void onFail(Object o) {
                mView.errorMsg(o.toString());
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void doLoginAdmin(String name, String account, String pwd) {
        RequestParams params = new RequestParams();
        params.put("account",name.replaceAll("\"",""));//商户信息
        params.put("merchant_name",account);//登录账号
        params.put("pwd",pwd); //密码
        HttpUtils.post(Config.APP_INTERFACE_BASE_URL+"/merchant.php?c=MerchantAccount&a=loginAdmin", params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                // 输出错误信息
//                LogUtils.e(TAG, "onFailure()--" + responseString);
            }

            @Override
            public void onFinish() {
                // 隐藏进度条
                super.onFinish();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject object = new JSONObject(responseString);
                    if(object.getInt("code")==2){
//                        showToast("登录聊天服务器失败");
                        mView.errorMsg(object.getString("msg"));
                    }else{
                        object=object.getJSONObject("data");
//                        loginHandle.sendEmptyMessage(0);
                        SPUtils.put(context,Config.LOGIN_ACCOUNT,name);
                        SPUtils.put(context,Config.MERCHANT_NAME,account);
                        SPUtils.put(context,Config.PWD,pwd);
//                    Config.AUTH=object.getString("auth");
//                    SPUtils.put(context,"auth",object.getString("auth"));
//                    SPUtils.put(context,Config.AUTH_RULE,object.getString("auth"));
                        Config.ADMIN_NAME=object.getString("truename");
                        SPUtils.put(context,Config.ADMIN_NAME,object.getString("service_nickname"));
                        SPUtils.put(context,Config.ADMIN_NAME_2,object.getString("truename"));
                        SPUtils.put(context,Config.TOKEN,object.getString("token"));
                        SPUtils.put(context,Config.UID,object.getString("merchant_id"));
                        SPUtils.put(context,Config.ADMIN_ID,"kf_"+object.getString("admin_id"));
                        SPUtils.put(context,Config.GROUP_ID,object.getString("merchant_group_id"));
//                    Config.ORDER_RATE=object.getString("order_rate");
//                    Config.SERVICE_ORDER_RATE=object.getString("service_order_rate");
                        mView.loginSuccess();
                    }
//                    //返回码
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {
                // 显示进度条
                super.onStart();
            }
        });
//        model.doLoginAdmin(name,account, pwd, new HttpOnNextListener() {
//            @Override
//            public void onNext(Object o) {
//                try {
//                    JSONObject object=new JSONObject(builder.create().toJson(o));
//                    SPUtils.put(context,Config.LOGIN_ACCOUNT,name);
//                    SPUtils.put(context,Config.MERCHANT_NAME,account);
//                    SPUtils.put(context,Config.PWD,pwd);
////                    Config.AUTH=object.getString("auth");
////                    SPUtils.put(context,"auth",object.getString("auth"));
////                    SPUtils.put(context,Config.AUTH_RULE,object.getString("auth"));
//                    Config.ADMIN_NAME=object.getString("truename");
//                    SPUtils.put(context,Config.ADMIN_NAME_2,object.getString("truename"));
//                    SPUtils.put(context,Config.TOKEN,object.getString("token"));
//                    SPUtils.put(context,Config.UID,object.getString("merchant_id"));
//                    SPUtils.put(context,Config.ADMIN_ID,"kf_"+object.getString("admin_id"));
//                    SPUtils.put(context,Config.GROUP_ID,object.getString("merchant_group_id"));
////                    Config.ORDER_RATE=object.getString("order_rate");
////                    Config.SERVICE_ORDER_RATE=object.getString("service_order_rate");
//                    mView.loginSuccess();
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                    mView.errorMsg("数据解析错误");
//                }
//            }
//
//            @Override
//            public void onFail(Object o) {
//                mView.errorMsg(o.toString());
//            }
//
//            @Override
//            public void onStart() {
//
//            }
//
//            @Override
//            public void onComplete() {
//
//            }
//        });
    }
}
